package com.xm.xmapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.xm.xmapp.R;
import com.xm.xmapp.adapter.ViewPagerAdapter;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.tool.Helper_SharedPreferences;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class GuideActivity extends BaseActivity {


    @Bind(R.id.guide_activity_viewpager)
    ViewPager guideActivityViewpager;
    private GestureDetector gestureDetector;
    private List<View> imgs;
    int currentItem;

    private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            float x = e2.getX() - e1.getX();
            if (x < 0 && currentItem == (imgs.size() - 1)) {
                Helper_SharedPreferences.set_bool_sp(StartActivity.YES_OR_NO, true);
                intent = new Intent(MyAppcation.getMyAppcation(), MainActivity.class);
                startActivity(intent);
                finish();
                return true;
            } else {
                return false;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gestureDetector = new GestureDetector(this, onGestureListener);
        setContentView(R.layout.activity_guide);
    }

    @Override
    public void initView(Bundle savedInstanceState) {

        imgs = new ArrayList<View>();

        ImageView img = new ImageView(this);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageResource(R.mipmap.a);
        imgs.add(img);

        img = new ImageView(this);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageResource(R.mipmap.b);
        imgs.add(img);

        img = new ImageView(this);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageResource(R.mipmap.c);
        imgs.add(img);

        img = new ImageView(this);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageResource(R.mipmap.d);
        imgs.add(img);


        guideActivityViewpager.setAdapter(new ViewPagerAdapter(imgs));
        guideActivityViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int arg0) {
                currentItem = arg0;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event)) {
            event.setAction(MotionEvent.ACTION_CANCEL);
        }
        return super.dispatchTouchEvent(event);
    }


    @OnClick(R.id.guide_activity_viewpager)
    public void onClick() {
    }
}
